﻿//     Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gateway.Console.Models.Validation;

namespace Gateway.Console.Models
{
    public class AnalyticsModel
    {
        public const string CreateClusterTag    = "CreateNew";
        public const string DividerTag          = "Divider";

        public AnalyticsModel()
        {
            this.StatusMessages = new List<AnalyticsStatus>();
        }

        [Key]
        public int Id { get; set; }

        [Display(Name = "Enable Application Analytics")]
        public bool EnableAnalytics { get; set; }

        public bool CreateCluster { get; set; }

        [Display(Name = "HDInsight Cluster Name")]
        public string ClusterName { get; set; }

        [NotMapped]
        [Display(Name = "New HDInsight Cluster Name")]
        [ConditionalRequired(ValidationMethod = "ValidateConditionalClusterName")]
        [Remote("CheckClusterNameAvailable", "Analytics", AdditionalFields = "ClusterName")]
        public string ClusterNameToCreate { get; set; }

        [Display(Name = "User Name")]
        [ConditionalRequired(ValidationMethod = "ValidateConditionalClusterName")]
        public string ClusterUserName { get; set; }

        [NotMapped]
        [Display(Name = "Password")]
        [ConditionalRequired(ValidationMethod = "ValidateConditionalClusterName")]
        [RegularExpression(@"^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$", 
            ErrorMessage = "The password must contain at least 10 characters and contain the following: an uppercase letter, a lowercase letter, a number, a special character")]
        public string ClusterPassword { get; set; }

        [Display(Name = "Node Count")]
        [ConditionalRequired(ValidationMethod = "ValidateConditionalClusterName")]
        public int ClusterNodeCount { get; set; }

        public string RefreshToken { get; set; }

        [NotMapped]
        [Display(Name = "HDInsight Cluster")]
        public IEnumerable<Tuple<string, string>> AvailableClusters { get; set; }

        [NotMapped]
        public IEnumerable<AnalyticsReportModel> Reports { get; set; }

        [NotMapped]
        public string AnalyticsServerHost { get; set; }

        [NotMapped]
        public bool Disabled { get; set; }

        public virtual ICollection<AnalyticsStatus> StatusMessages { get; set; }

        static bool ValidateConditionalClusterName(object model)
        {
            return ((AnalyticsModel)model).ClusterName != CreateClusterTag;
        }
    }
}
